/**
 *
 */
package com.ybkj.daijia.server.service.impl;


import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.WeixinManagerXOrderMapper;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.WeixinManagerXOrder;
import com.ybkj.daijia.server.service.WeixinManagerXOrderService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Shine
 *
 */
@Service
public class DefaultWeixinManagerXOrderService implements WeixinManagerXOrderService {

    @Autowired
    private WeixinManagerXOrderMapper weixinManagerXOrderMapper;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public WeixinManagerXOrder findByOrderId(Long orderId) {

        return weixinManagerXOrderMapper.selectByOrderId(orderId);
    }


    @Override
    public Page<WeixinManagerXOrder> queryByManagerId(final Long managerId) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<WeixinManagerXOrder> content = weixinManagerXOrderMapper
            .selectByManagerId(managerId, Pager.getOffset(), Pager.getLimit());
        long total = weixinManagerXOrderMapper.countByManagerId(managerId);

        return new PageImpl<WeixinManagerXOrder>(content, pageable, total);
    }

    @Override
    public Page<Map<String, Object>> queryByManagerId(Long managerId, Integer page, Integer limit) {

        Pageable pageable = new PageRequest(page, limit);

        List<Order> content = orderMapper.selectByManager(managerId, page * limit, limit);
        long total = orderMapper.countByManager(managerId);

        List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();

        if (total > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            for (Order order : content) {
                Map<String, Object> map = new HashMap<String, Object>();

                map.put("orderId", order.getId());
                map.put("orderNumber", order.getOrderNumber());

                if (null != order.getFinishTime()) {
                    map.put("finishedTimeStr", sdf.format(order.getFinishTime()));
                }
                map.put("status", order.getStatus());
                map.put("startPlace", order.getFromPlace());
                map.put("endPlace", order.getToPlace());
                map.put("serverTimeStr", sdf.format(order.getServerTime()));
                map.put("treatment", order.isTreatment());
                maplist.add(map);
            }
        }

        return new PageImpl<Map<String, Object>>(maplist, pageable, total);
    }


    @Override
    public BigDecimal sumMoney(Long managerId, Date start, Date end) {
        return weixinManagerXOrderMapper.sumMoney(managerId, start, end);
    }

}